package com.cat.dataStructure;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-mirror-score-of-a-string/
 * @create 2025/9/8 18:57
 * @since JDK17
 */

public class Solution60 {
    public long calculateScore(String s) {
        int n = s.length();
        long ans = 0;
        List<List<Integer>> list = new ArrayList<>();
        IntStream.range(0, 26).forEach(o -> list.add(new ArrayList<>()));
        for (int i = 0; i < n; i++) {
            int a = s.charAt(i) - 'a', b = 25 - a;
            if (list.get(b).size() == 0) {  // 没有对应
                list.get(a).add(i); // 把i加入
            } else {    // 有
                var t = list.get(b);
                ans += i - t.get(t.size() - 1);
                t.remove(t.size() - 1);
            }
        }

        return ans;
    }
}
